package com.hengyue.system.service.impl;

import com.hengyue.system.domain.entity.SysLogininfor;
import com.hengyue.system.repository.SysLoginInfoRepository;
import com.hengyue.system.service.ISysLogininforService;
import jakarta.persistence.criteria.Predicate;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author ruoyi
 */
@Service
@RequiredArgsConstructor
public class SysLogininforServiceImpl implements ISysLogininforService {


    private final SysLoginInfoRepository repository;

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        repository.save(logininfor);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public Page<SysLogininfor> selectLogininforList(SysLogininfor logininfor, Pageable pageable) {
        Specification<SysLogininfor> spec = getSpecification(logininfor);
        return repository.findAll(spec, pageable);
    }

    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
        Specification<SysLogininfor> spec = getSpecification(logininfor);
        return repository.findAll(spec);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     */
    @Override
    public void deleteLogininforByIds(List<Long> infoIds) {
        repository.deleteAllById(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor() {
        repository.deleteAllInBatch();
    }

    public Specification<SysLogininfor> getSpecification(SysLogininfor logininfor) {
        return (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(logininfor.getIpaddr())) {
                predicates.add(cb.like(root.get("ipaddr"), "%" + logininfor.getIpaddr() + "%"));
            }
            if (StringUtils.isNotBlank(logininfor.getStatus())) {
                predicates.add(cb.equal(root.get("status"), logininfor.getStatus()));
            }
            if (StringUtils.isNotBlank(logininfor.getUserName())) {
                predicates.add(cb.like(root.get("userName"), "%" + logininfor.getUserName() + "%"));
            }
            if (logininfor.getParams() != null) {
                LocalDateTime beginTime = logininfor.getParams().get("beginTime");
                LocalDateTime endTime = logininfor.getParams().get("endTime");
                if (beginTime != null) {
                    predicates.add(cb.greaterThanOrEqualTo(root.get("loginTime"), beginTime));
                }
                if (endTime != null) {
                    predicates.add(cb.lessThanOrEqualTo(root.get("loginTime"), endTime));
                }
            }
            query.orderBy(cb.desc(root.get("id")));
            return cb.and(predicates.toArray(new Predicate[0]));
        };
    }
}
